package com.meitianhui.convenitenbuy.module.order.ui.fragment;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.widget.TextView;

import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.base.BaseFragment;
import com.meitianhui.convenitenbuy.main.adapter.FragmentAdapter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


/**
   Created by amy on 2018/1/13.
 */

public class HuiGuoOrderFragment extends BaseFragment {

    private String[] mTitles = new String[]{"全部", "待付款", "待返款", "已返款", "已取消"};
    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    private List<String> mTitleList = new ArrayList<>();
    private List<Fragment> mFragments;
    FragmentAdapter adapter;

    @Override
    protected int getLayout() {
        return R.layout.fragment_huiguo_order;
    }

    @Override
    protected void initView(LayoutInflater inflater) {
        super.initView(inflater);
        mTabLayout = (TabLayout) getViewById(R.id.hg_tabLayout);
        mViewPager = (ViewPager) getViewById(R.id.hg_viewPager);
        initData();
    }
    private void initData() {
        int len = mTitles.length;
        Collections.addAll(mTitleList, mTitles);
        mFragments = new ArrayList<>();
        for (int i = 0; i < len; i++) {
            Bundle bundle = new Bundle();
            if (i == 0) {
                bundle.putString("status", "");
            } else if (i == 1) {      //待确认
                bundle.putString("status", "processing");
            } else if (i == 2) {  //待返款
                bundle.putString("status", "confirmed");
            } else if (i == 3) {    //已返款
                bundle.putString("status", "closed");
            } else if (i == 4) {    //已取消
                bundle.putString("status", "cancelled");
            }
            HuiGuoOrderTypeFragment fragment = new HuiGuoOrderTypeFragment();
            fragment.setArguments(bundle);
            mFragments.add(fragment);
        }

        if(adapter==null){
            adapter = new FragmentAdapter(getChildFragmentManager(), mFragments, mTitleList);
            mViewPager.setAdapter(adapter);//给ViewPager设置适配器
        }
        mTabLayout.setupWithViewPager(mViewPager);//将TabLayout和ViewPager关联起来
        mViewPager.setOffscreenPageLimit(4);
        mTabLayout.setSelectedTabIndicatorHeight(0);
        try {
            for (int i = 0; i < mTitleList.size(); i++) {
                //获得到对应位置的Tab
                TabLayout.Tab itemTab = mTabLayout.getTabAt(i);
                if (itemTab != null) {
                    //设置自定义的标题
                    itemTab.setCustomView(R.layout.item_tab);
                    TextView textView = (TextView) itemTab.getCustomView().findViewById(R.id.tv_name);
                    textView.setText(mTitleList.get(i));
                }
            }
            mTabLayout.getTabAt(0).getCustomView().setSelected(true);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void call(Object o) {

    }
}
